#include "wuFilmXML.h"

wuFilmXML::wuFilmXML()
{
}

wuFilmXML::~wuFilmXML()
{
}


void wuFilmXML::setup( string _filename, vector <wuSkin> * _skins, bool _bAuto)
{
	// recullo el punter al vector d' skins
	skins = _skins;


	XML.loadXML(_filename);

	bGo=false;
	bPreCaptura = true;
	bLoop = false;
    bDragOn = false;
	XML.goto0();

	framePos.x=0;
	framePos.y=0;
	framePos_ini.x=0;

    bAuto = _bAuto;
    if(_bAuto)
        setSkinPos(); // coloca el frame on son els skins

    scrollOffset=0;

}

void wuFilmXML::update()
{
    if(bGo && XML.getCurrentFrame()==XML.getNumFrames()-1)
    {
        if(bLoop)
        {
            int x_tmp = 0;

            for(int i=0; i< frame.size(); i++)
            {
                x_tmp = x_tmp + frame[i].pos.x;
            }
            if(x_tmp<0)
            {
                x_tmp = abs(x_tmp) / frame.size();
                x_tmp = 0 - x_tmp;
            }else
                x_tmp = x_tmp / frame.size();

            framePos.x = framePos.x + x_tmp - framePos_ini.x - scrollOffset;
            XML.goto0();
        }
    }

 	if(bGo)
		updateFrame(XML.getNextFrame());

	if(bPreCaptura)
    {
        if(bAuto)
            rePositioning();
        else
            updateFrame(XML.getFrame(0));
    }
}

void wuFilmXML::draw(bool bDrawlines, int _offX)
{
 	ofSetColor(255, 255, 255);

	if((bDrawlines && bPreCaptura ) || (bAuto && bPreCaptura ) || (bGo && skins->size() > 0))
	{
//            for(int i=0; i < skins->size(); i++)
            for(int i=0; i < frame.size(); i++)
            {
                float ang = frame[i].ang;
                ofPoint pos = frame[i].pos+framePos;

                int j=i%(skins->size());

                // draw Skin at position
                ofPushMatrix();
                    ofTranslate(pos.x+_offX, pos.y);
                    (*skins)[j].skin.setAnchorPercent(0.5, 0.5);
                    ofRotateZ(ofRadToDeg(ang));
                    ofEnableAlphaBlending();
                    if (bDrawlines && bPreCaptura)
                        ofSetColor(255,100,100,150);
                    (*skins)[j].skin.draw(0, 0);
                    ofDisableAlphaBlending();
                ofPopMatrix();
            }
	}

}

void wuFilmXML::updateFrame(vector<pieceInfo> * _newframe)
{
		frame.clear();

		for(int i=0; i < (*_newframe).size(); i++)
		{
			pieceInfo piece;

			piece.pos = (*_newframe)[i].pos;
			piece.pos.x = piece.pos.x+scrollOffset;
			piece.ang = (*_newframe)[i].ang;
			// add piece position to frame
			frame.push_back(piece);
		}
}

void wuFilmXML::dragOff()
{
    if(bPreCaptura)
        bDragOn = false;
}

bool wuFilmXML::drag(int _x,int _y)
{
    if(bPreCaptura)
    {
       if(!bDragOn)
        {
            offsetX = _x - framePos.x;
            offsetY = _y - framePos.y;
            bDragOn = true;
        }

        framePos.x = _x -offsetX;
        framePos.y = _y -offsetY;
    }

	    return bDragOn;
}

void wuFilmXML::clear()
{
	bGo=false;
	bPreCaptura = false;
}
void wuFilmXML::go(ofRectangle _rect)
{
    get_framePos_ini();
    framePos.x = _rect.x + _rect.width/2 - framePos_ini.x - scrollOffset;
    framePos.y = _rect.y + _rect.height/2 - framePos_ini.y;

    bGo = true;
    bPreCaptura = false;
}

void wuFilmXML::autoPosition(bool _bAuto)
{
    bAuto = _bAuto;
}
void wuFilmXML::rePositioning()
{
    bool bDone=true;
    int inc=5;

    vector<pieceInfo> * frame0 = XML.getFrame(0);

	for(int i=0; i < (*frame0).size(); i++)
	{
        if( abs(frame[i].pos.x - (*frame0)[i].pos.x-scrollOffset) > inc)
        {
            bDone = false;
            if (frame[i].pos.x < (*frame0)[i].pos.x+scrollOffset)
                frame[i].pos.x=frame[i].pos.x+inc;
            if (frame[i].pos.x > (*frame0)[i].pos.x+scrollOffset)
                frame[i].pos.x=frame[i].pos.x-inc;
        }
        if( abs(frame[i].pos.y - (*frame0)[i].pos.y) >inc)
        {
            bDone = false;
            if (frame[i].pos.y < (*frame0)[i].pos.y)
                frame[i].pos.y=frame[i].pos.y+inc;
            if (frame[i].pos.y > (*frame0)[i].pos.y)
                frame[i].pos.y=frame[i].pos.y-inc;
        }
        if( abs(frame[i].ang - (*frame0)[i].ang) > 0.3)
        {
            bDone = false;
            if (frame[i].ang < (*frame0)[i].ang)
                frame[i].ang=frame[i].ang+0.3;
            if (frame[i].ang > (*frame0)[i].ang)
                frame[i].ang=frame[i].ang-0.3;
        }

           frame[i].ang = (*frame0)[i].ang;
 		// add piece position to frame
	}

    // els skins extres surten de la pantalla
	for(int i=(*frame0).size(); i < frame.size(); i++)
	{
        if( frame[i].pos.x > -2)
        {
            bDone = false;
            frame[i].pos.x=frame[i].pos.x-inc;
        }
        if( frame[i].pos.y > -2)
        {
            bDone = false;
            frame[i].pos.y=frame[i].pos.y-inc;
        }
	}

	if(bDone)
	{
	    get_framePos_ini();
	    bGo = true;
	    bPreCaptura = false;
	}
}

void wuFilmXML::setSkinPos()
{
		frame.clear();

		for(int i=0; i < (*skins).size(); i++)
		{
			pieceInfo piece;

			piece.pos = (*skins)[i].posSkin;
			piece.ang = (*skins)[i].angSkin;
			// add piece position to frame
			frame.push_back(piece);
		}
}

void wuFilmXML::setScrollOffset(int _scrollOff)
{
    scrollOffset = _scrollOff;
}

void wuFilmXML::setLoop(bool _bLoop)
{
    bLoop = _bLoop;
}

void wuFilmXML::get_framePos_ini()
{
    vector<pieceInfo> * frame0 = XML.getFrame(0);

    framePos_ini.x=0;
    framePos_ini.y=0;

    for(int i=0; i< (*frame0).size(); i++)
    {
        framePos_ini.x = framePos_ini.x + (*frame0)[i].pos.x;
        framePos_ini.y = framePos_ini.y + (*frame0)[i].pos.y;
    }
    framePos_ini.x = framePos_ini.x / (*frame0).size();
    framePos_ini.y = framePos_ini.y / (*frame0).size();
}
